﻿using System.ComponentModel.Design;
using System.Globalization;
using Microsoft.VSSDK.Tools.VsIdeTesting;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.VsSDK.IntegrationTestLibrary;
using ThreadSafe.JSense.VSIX;

namespace ThreadSafe.JSense.VSIX_IntegrationTests
{
   [TestClass]
   public class MenuItemTest
   {
      /// <summary>
      ///Gets or sets the test context which provides
      ///information about and functionality for the current test run.
      ///</summary>
      public TestContext TestContext { get; set; }

      /// <summary>
      ///A test for lauching the command and closing the associated dialogbox
      ///</summary>
      [TestMethod]
      [HostType("VS IDE")]
      public void LaunchCommand()
      {
         UIThreadInvoker.Invoke((ThreadInvoker)
            delegate
               {
                  var menuItemCmd =
                     new CommandID(GuidList.GuidThreadSafeJSenseVsixCmdSet,
                        (int) PkgCmdIDList.CmdIdJSense);

                  // Create the DialogBoxListener Thread.
                  var expectedDialogBoxText =
                     string.Format(CultureInfo.CurrentCulture,
                        "{0}\n\nInside {1}.MenuItemCallback()",
                        "ThreadSafe.JSense.VSIX",
                        "PatrikMelanderThreadSafeInc.ThreadSafe_JSense_VSIX.ThreadSafe_JSense_VSIXPackage");
                  var purger = new DialogBoxPurger(NativeMethods.IDOK,
                     expectedDialogBoxText);

                  try
                  {
                     purger.Start();

                     var testUtils = new TestUtils();
                     testUtils.ExecuteCommand(menuItemCmd);
                  }
                  finally
                  {
                     Assert.IsTrue(purger.WaitForDialogThreadToTerminate(),
                        "The dialog box has not shown");
                  }
               });
      }

      #region Nested type: ThreadInvoker

      private delegate void ThreadInvoker();

      #endregion
   }
}
